package com.fingard.dsp.bank.directbank.boqh01;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.xml.XmlTextReader;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName BOQH01Base
 * @Author Zhengzy
 * @Date 2020/3/12 13:56
 * @Version 1.0
 **/
public class BOQH01Base extends DirectBase {
    /**
     * 发送指令到银行前置
     * @param sendData
     * @param bsnCode 接口指令 如7506
     * @return
     */
    protected String[] sendToBank(String sendData, String bsnCode) throws UnsupportedEncodingException {
        String[] bankRetStr = new String[]{"", ""};
        String code = bsnCode;//7506 7515 7516 190801 190802 1944所对应的银行指令
        sendData = String.format("%s|#%s", code, sendData);
        WriteBankLogStep2(sendData);
        String ip = bankFront.serverIP;// 待配置
        String port = bankFront.serverPort;// 待配置
        String tmpAddrUrl = "http://" + ip + ":" + port + "/corlink/httpAccess";
        WriteBankLogLn("2.提交到银行的地址：" + tmpAddrUrl);
        WebRequest tmpWebRequest = new WebRequest(tmpAddrUrl);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Method", "POST");
        String charset = "gb2312";
        tmpWebRequest.setRequestProperty("Content-Type", "text/xml;charset=" + charset);
        tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
        byte[] tmpUpBytes = sendData.getBytes(getCharset());
        bankRetStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(bankRetStr[1]);
        return bankRetStr;
    }

    /**
     * 解析银行的返回
     * @param bankReturn
     * @return
     */
    protected String[] IsResultInvlid(String bankReturn) {
        String[] sArray = bankReturn.split("#");
        String[] retArray = {"", "", "", ""};// 分别对应{RespCode，RespInfo，boolean, 报文}
        if (sArray.length >= 2) {
            String[] tmp = sArray[0].split("|");
            if (tmp.length == 2) {
                if (tmp[0] != "0000") {
                    retArray[0] = "0";
                    retArray[1] = "";
                    retArray[2] = "true";
                    return retArray;
                }
            }
        }
        String respXml = bankReturn.substring(bankReturn.indexOf("#") + 1);
        if (respXml.length() > 0 && respXml.contains("<")){
                // 把respXml变成xml报文
                retArray[0] = "0";
                retArray[1] = "";
                retArray[2] = "true";
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankReturn, tmpSbMsg);
                if (tmpBkReader != null) {
                    retArray[3] = respXml;
                    return retArray;
                } else {
                    WriteBankLogLn("加载银行返回报文失败：" + tmpSbMsg);
                    retArray[0] = "-1";
                    retArray[1] = "加载银行返回报文失败";
                    retArray[2] = "false";
                    return retArray;
                }
        }
        retArray[0] = "-999";
        retArray[1] = bankReturn;
        retArray[2] = "false";
        return retArray;
    }

    /**
     * 解析银行返回签名
     * @param bankReturn
     * @return
     */
  /*  protected String[] IsResultInvlidForSign(String bankReturn){
        String[] retArray = {"", "", "", ""};// 分别对应{RespCode，RespInfo，boolean,报文}

            retArray[0] = "0";
            retArray[1] = "";
            retArray[2] = "true";
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(bankReturn, tmpSbMsg);
            if (tmpBkReader != null) {
                retArray[3] = bankReturn;
                return retArray;
            } else {
                WriteBankLogLn("加载银行返回签名失败：" + tmpSbMsg);
                retArray[0] = "-1";
                retArray[1] = "加载银行返回签名失败";
                retArray[2] = "false";
                return retArray;
            }
    }*/

    /**
     * 发送到前置机进行签名
     * @param sendData
     * @return
     */
    protected String SendTranInfoToBankForSign(String sendData) throws UnsupportedEncodingException {
        String signedData = "";
        String signURL = bankFront.signURL;
        WriteBankLogLn("1.银行签名地址：" + signURL);
        WriteBankLogStep2(sendData);
        WebRequest tmpWebRequest = new WebRequest(signURL);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Content-Length", String.valueOf(sendData.length()));
        tmpWebRequest.setRequestProperty("Content-Type", "INFOSEC_SIGN/1.0");
        String charsetName = getCharset();
        byte[] tmpToWriteBytes = sendData.getBytes(charsetName);// 字符串转byte数组
        String[] retSign = tmpWebRequest.upload(tmpToWriteBytes, "gb2312");
        if (retSign[0].length() == 0) {
            try {
                WriteBankLogLn("3.银行返回签名：" + retSign[1]);
                XmlTextReader reader = new XmlTextReader(retSign[1]);
                String tmpRest = reader.getSimpleText("result");
                if ("0".equals(tmpRest)) {
                    WriteBankLogLn("签名成功");
                    signedData = reader.getSimpleText("sign");
                } else {
                    WriteBankLogLn("签名失败");
                }
            }catch (Exception e){
                WriteBankLogLn(e.getMessage());
            }
        } else {
            WriteBankLogLn("与银行签名端口通讯失败；详细信息：" + retSign[1]);
        }
        return signedData;
    }

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }
}
